/*----------------------------------------------------------------------------*\
|
|						Public Interface to Muse Technology
|
|							     www.Muse.com
| Author : SunJun 
| Email : sundoom@msn.com
\*----------------------------------------------------------------------------*/

//////////////////////////////////////////////////////////////////////////
// $Id: MThread.h,v 1.1 2009/04/03 12:46:09 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#ifndef __MThread__
#define __MThread__

//////////////////////////////////////////////////////////////////////////

#if defined __cplusplus

//////////////////////////////////////////////////////////////////////////

#include <Muse.h>
#include <MReference.h>

//////////////////////////////////////////////////////////////////////////

NAMESPACE_BEGIN

//////////////////////////////////////////////////////////////////////////

	typedef MPtr<class MThread>MThreadPtr;

	//////////////////////////////////////////////////////////////////////////

	// this class provides an object-oriented thread interface.
	class API MThread : public virtual MReference
	{
	public:
		// enumerated type for thread priority
		enum PriorityType 
		{
			THREAD_PRIORITY_MAX,      // The maximum possible priority
			THREAD_PRIORITY_HIGH,     // A high (but not max) setting
			THREAD_PRIORITY_NOMINAL,  // An average priority
			THREAD_PRIORITY_LOW,      // A low (but not min) setting
			THREAD_PRIORITY_MIN,      // The miniumum possible priority
			THREAD_PRIORITY_DEFAULT   // Priority scheduling default
		};
		
	public:		
		// constructor
		MThread();
		
		// destructor
		virtual ~MThread();		

		// check whether a thread is still alive.
		INLINE BOOL alive() const;

		// suspend the execution of a particular thread.
		INLINE BOOL suspend() const;

		// continue the execution of a previously suspended thread.
		INLINE BOOL resume() const;

	protected:
		// start the thread
		INLINE VOID start( U32 size = 0, U32 priority = THREAD_PRIORITY_NOMINAL );

		// end the thread
		INLINE VOID end();

		// thread's run method.  must be implemented by derived classes.
		// this is where the action happens.
		virtual VOID run() = 0;

	private:
		// the thread hook
		static U32 STDCALL hook( VOID * arg );

		// set the thread is done
		INLINE VOID done();

	private:
		MMutex	mMutex;
		HANDLE	mHandle;
		BOOL	mRunning;
	};	

//////////////////////////////////////////////////////////////////////////

NAMESPACE_END

//////////////////////////////////////////////////////////////////////////

#endif // __cplusplus

//////////////////////////////////////////////////////////////////////////

#endif // __MThread__

//////////////////////////////////////////////////////////////////////////